package com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 通过context对象获取wen应用初始化参数
 */
@WebServlet("/sc03.do")
public class ServletContextDemo03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取ServletContext对象
        ServletContext context = getServletContext();
        // 2、通过context对象获取初始化参数
        String username = context.getInitParameter("username");
        System.out.println(username);

        // 获取所有初始化参数的名字枚举
        Enumeration<String> names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            // 获取初始化参数名字
            String name = names.nextElement();
            String value = context.getInitParameter(name);
            System.out.println(value);
        }
    }
}
